<?php
/**
 * I-Cafe framework
 * 
 * @category CF
 * @package CF_Db
 * @link http://www.i-cafe.org
 * @license http://www.i-cafe.org/license
 * @version 04-08-2010
 * @author tcl_java@yahoo.com
 */

class CF_Db_Create_Database
{
	/**
	 * 
	 * @var string
	 */
	const TYPE_DATABASE = 'DATABASE';
	
	/**
	 * 
	 * @var string
	 */
	const TYPE_SCHEMA = 'SCHEMA';
	
	/**
	 * 
	 * @var string
	 */
	protected $_type;
	
	/**
	 * 
	 * @var string
	 */
	protected $_name;
	
	/**
	 * 
	 * @var string
	 */
	protected $_charset;
	
	/**
	 * 
	 * @var string
	 */
	protected $_collation;
	
	/**
	 * 
	 */
	public function __construct()
	{
		$this->reset();	
	}
	
	/**
	 * 
	 */
	public function reset()
	{
		$this->_type 		= 'DATABASE';
		$this->_name 		= null;
		$this->_charset 	= 'utf8';
		$this->_collation	= 'utf8_general_ci';
	}
	
	/**
	 * 
	 * @param string $type
	 */
	public function type($type)
	{
		$this->_type = $type;
		return $this;
	}
	
	/**
	 * 
	 * @param string $name
	 */
	public function name($name)
	{
		$this->_name = $name;
		return $this;
	}
	
	/**
	 * 
	 * @param string $charset
	 */
	public function charset($charset)
	{
		$this->_charset = $charset;
		return $this;
	}
	
	/**
	 * @param string $collation
	 *
	 */
	public function collation($collation)
	{
		$this->_collation = $collation;
		return $this;
	}
	
	/**
	 * 
	 *
	 */
	public function execute()
	{
		CF_Db::getInstance()->query($this->__toString());
		return CF_Db::getInstance()->getAffectedRows();
	}
	
	/**
	 * 
	 *
	 */
	public function __toString()
	{
		$parts = array(
			'CREATE',
			$this->_type,
			$this->_name,
			'CHARACTER SET',
			$this->_charset,
			'COLLATE',
			$this->collation
		);
		
		return implode(' ', $parts);
	}
}